package com.web.page;

import com.web.annotations.Copy;
import com.web.annotations.PageElement;
import com.web.annotations.PageUrl;
import com.web.imp.AbstractWebPage;
import com.web.lang.Language;
import com.web.lang.LanguageConfig;
import com.web.model.element.WebButton;
import com.web.model.element.WebCheckBox;
import com.web.model.element.WebLink;
import com.web.model.element.WebTextField;
import com.web.page.RegisterForFree;

@PageUrl("/user/login/")
public class Login extends AbstractWebPage{

	@Copy(id = "title_login")
	public static String TITLE_COPY;
	
	@Copy(id = "user_o_pass_invalid")
	public static String INCORRECT_EMAIL_OR_PASSWORD_COPY;
	
	@Copy(id = "isEmpty")
	public static String BLANK_FIELDS_COPY;
	
	@Copy(id = "user")
	public static String USER_COPY;
	
	@Copy(id = "password")
	public static String PASSWORD_COPY;
	
	@Copy(id = "forgot_password")
	public static String FORGOT_PASSWORD_COPY;
	
	@Copy(id = "iniciar_automatico")
	public static String STAY_LOGGED_ALL_DAY_COPY;
	
	@Copy(id = "login")
	public static String LOGIN_COPY;
	
	@Copy(id = "no_have_account")
	public static String NO_HAVE_ACCOUNT_COPY;
	
	@Copy(id = "register_free")
	public static String REGISTER_FREE_COPY;

	
	@PageElement(id = "user")
	private WebTextField userField;
	
	@PageElement(id = "password")
	private WebTextField passwordField;
	
	@PageElement(id = "add_login")
	private WebButton startSession;
	
	@PageElement(id = "sesion_automatica")
	private WebCheckBox stayLoggedIn;
	
	@PageElement(id = "logo")
	private WebLink logo;
	
	@PageElement(id = "forgot_password")
	private WebLink forgotPassword;
	
	@PageElement(id = "register_free")
	private WebLink registerForFree;
	
	
	@PageElement(className = "down-arrow")
	private WebLink spanLanguage;
	
	@PageElement(id = "en")
	private WebLink englishLink;
	
	@PageElement(id = "es")
	private WebLink spanishLink;
	
	@PageElement(id = "ca")
	private WebLink catalanLink;
	
	@PageElement(id = "fr")
	private WebLink frenchLink;
	
	@PageElement(id = "br")
	private WebLink portugueseLink;
	
	@PageElement(id = "ja")
	private WebLink japaneseLink;
	
	@PageElement(id = "it")
	private WebLink italianLink;
	
	@PageElement(id = "de")
	private WebLink germanLink;
	
	public static String getTitle(){
		
		return TITLE_COPY + " - " + Index.TITLE;
	}
	
	public String getUrl(){
		
		Language language = LanguageConfig.getLanguage();
		String url = this.url + "?lang=" + language;
		
		return url;
	}
	
	public Index login(String user, String password){
		
		userField.sendKeys(user);
		passwordField.sendKeys(password);
		startSession.click();
		
		return browser.goTo(Index.class);
	}
	
	public void loginError(String user, String password){
		
		userField.sendKeys(user);
		passwordField.sendKeys(password);
		startSession.click();
	}	
	
	public Index loginAndStayLogged(String user, String password){
		
		userField.sendKeys(user);
		passwordField.sendKeys(password);
		stayLoggedIn.check();
		startSession.click();
		
		return browser.goTo(Index.class);
	}
	
	public void loginAndStayLoggedError(String user, String password){
		
		userField.sendKeys(user);
		passwordField.sendKeys(password);
		stayLoggedIn.check();
		startSession.click();
	}	
	
	public void clickLogoToHome(){
		logo.click();
	}

	public ForgotPassword goToForgotPassword(){

		forgotPassword.click();
		return browser.goTo(ForgotPassword.class);
	}
	
	public RegisterForFree goToRegisterForFreePage() {
		
		registerForFree.click();
		return browser.goTo(RegisterForFree.class);
	}
	
	public void selectEnglishLanguage(){
		spanLanguage.click();
		englishLink.click();
	}
	
	public void selectSpanishLanguage(){
		spanLanguage.click();
		spanishLink.click();
	}
	
	public void selectFrenchLanguage(){
		spanLanguage.click();
		frenchLink.click();
	}
	
	public void selectCatalanLanguage(){
		spanLanguage.click();
		catalanLink.click();
	}
	
	public void selectGernamLanguage(){
		spanLanguage.click();
		germanLink.click();
	}
	
	public void selectJapaneseLanguage(){
		spanLanguage.click();
		japaneseLink.click();
	}
	
	public void selectPortugueseLanguage(){
		spanLanguage.click();
		portugueseLink.click();
	}
	
	public void selectItalianLanguage(){
		spanLanguage.click();
		italianLink.click();
	}
}
